package com.xth8013.as.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xth8013.as.domain.Menu;
import com.xth8013.as.service.MenuService;
import com.xth8013.as.util.Message;
import com.google.gson.Gson;

/**
 * 
 * @Description:
 * @author: tianhua.xie
 * @Date: 2017年4月3日 下午9:39:15
 *
 */
@Controller
@RequestMapping("/menuMgrController")
public class MenuMgrController extends BaseController {

	@Autowired
	private MenuService menuService;

	@RequestMapping("/menuMgr")
	public String toMenuMgrPage() {
		return "menu/menu_mgr";
	}

	@RequestMapping(value = "/loadMenuTreeGrid")
	@ResponseBody
	public Map<String, Object> loadMenuTreeGrid(HttpServletRequest request) {
		Map<String, Object> treeData = this.menuService.loadMenuTreeGrid();
		Gson gson = new Gson();
		System.out.println(gson.toJson(treeData));
		return treeData;
	}

	@RequestMapping(value = "/preAdd")
	public ModelAndView preAddMenu(ModelAndView mv) {
		// System.out.println("preAddMenu");
		mv.setViewName("menu/menu_add");

		Menu menu = new Menu();
		menu.setMenuId(menuService.getNewMenuIdByParentId(0L));
		menu.setGrade(1);
		mv.addObject("menu", menu);

		return mv;
	}

	@RequestMapping(value = "/preAdd/{parentId}")
	public ModelAndView preAddSunMenu(@PathVariable(value = "parentId") Long parentId, ModelAndView mv) {
		// System.out.println("preAddSunMenu parentId = " + parentId);
		mv.setViewName("menu/menu_sun_add");

		Menu parentMenu = menuService.getMenuById(parentId);
		Menu menu = new Menu();

		menu.setMenuId(menuService.getNewMenuIdByParentId(parentId));
		menu.setGrade(parentMenu.getGrade() + 1);
		mv.addObject("menu", menu);
		mv.addObject("parentMenu", parentMenu);

		return mv;
	}

	@RequestMapping(value = "/addMenu")
	@ResponseBody
	public Message save(Menu menu) throws Exception {

		Message message = null;

		System.out.println(menu);

		try {
			this.menuService.save(menu);
			message = coverMessage("200", "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			message = coverMessage("500", e.getMessage());
		}

		return message;
	}

	@RequestMapping(value = "/preModify/{menuId}")
	public String preModify(@PathVariable(value = "menuId") Long menuId, ModelMap modelMap) {

		Menu menu = this.menuService.getMenuById(menuId);
		String parentMenuName = null;

		if (menu.getParentId() != 0) {
			Menu parentMenu = menuService.getMenuById(menu.getParentId());
			parentMenuName = parentMenu.getMenuName();
		} else {
			parentMenuName = "无";
		}

		modelMap.put("menu", menu);
		modelMap.put("parentMenuName", parentMenuName);
		return "menu/menu_edit";
	}

	@RequestMapping(value = "/modifyMenu")
	@ResponseBody
	public Message modifyMenu(Menu menu) throws Exception {

		Message message = null;

		try {
			this.menuService.modifyMenuSomeInfo(menu);
			message = coverMessage("200", "修改成功");
		} catch (Exception e) {
			message = coverMessage("500", e.getMessage());
			e.printStackTrace();
		}

		return message;
	}

	@RequestMapping("/deleteMenu")
	@ResponseBody
	public Message deleteMenu(@RequestParam("delMenuId") Long delMenuId) throws Exception {

		Message message = null;

		try {
			this.menuService.delectMenuById(delMenuId);
			message = coverMessage("200", "删除成功");
		} catch (Exception e) {
			message = coverMessage("500", e.getMessage());
			e.printStackTrace();
		}

		return message;
	}

}
